const { defineConfig } = require('@vue/cli-service')
const timeCode = new Date().getTime();
module.exports = defineConfig({
	//设置为空打包后不分更目录还是多级目录
	publicPath: '',
	//build编译后存放静态文件的目录
	//assetsDir: "static",

	// build编译后不生成资源MAP文件
	productionSourceMap: false,

	//开发服务,build后的生产模式还需nginx代理
	devServer: {
    allowedHosts: 'all',
		host: '0.0.0.0',
		open: false, //运行后自动打开浏览器
		port: process.env.VUE_APP_PORT, //挂载端口
		hot: process.env.NODE_ENV=='development'?true:false,
		proxy: {
			'/cmsapi': {
				target: process.env.VUE_APP_API_BASEURL,
				ws: false,//socket
				secure: false,// url是否https
				changeOrigin: true, //是否跨域
				pathRewrite: {
					'^/cmsapi': ''
				},
				onProxyRes(proxyRes,req) {
					const realUrl = new URL(req.url || '', process.env.VUE_APP_API_BASEURL);
					proxyRes.headers['x-real-url'] = realUrl;
				}
			}
			// '/cmsapi/ms': {
			// 	target: 'http://192.168.51.181:8081',
			// 	changeOrigin: true, //是否跨域
			// 	pathRewrite: {
			// 		'^/cmsapi/ms': '/ms'
			// 	},
			// 	onProxyRes(proxyRes,req) {
			// 		const realUrl = new URL(req.url || '', 'http://192.168.51.181:8081');
			// 		proxyRes.headers['x-real-url'] = realUrl;
			// 	}
			// },
			// '/cmsapi/cms': {
			// 	target: 'http://192.168.51.181:8080',
			// 	ws: false,//socket
			// 	secure: false,// url是否https
			// 	changeOrigin: true, //是否跨域
			// 	pathRewrite: {
			// 		'^/cmsapi/cms': '/cms'
			// 	},
			// 	onProxyRes(proxyRes,req) {
			// 		const realUrl = new URL(req.url || '', 'http://192.168.51.181:8080');
			// 		proxyRes.headers['x-real-url'] = realUrl;
			// 	}
			// },
			// '/cmsapi/api': {
			// 	target: 'http://192.168.51.181:8081',
			// 	ws: false,//socket
			// 	secure: false,// url是否https
			// 	changeOrigin: true, //是否跨域
			// 	pathRewrite: {
			// 		'^/cmsapi/api': '/api'
			// 	},
			// 	onProxyRes(proxyRes,req) {
			// 		const realUrl = new URL(req.url || '', 'http://192.168.51.181:8081');
			// 		proxyRes.headers['x-real-url'] = realUrl;
			// 	}
			// },
			// '/cmsapi/web': {
			// 	target: 'http://192.168.51.181:8080',
			// 	ws: false,//socket
			// 	secure: false,// url是否https
			// 	changeOrigin: true, //是否跨域
			// 	pathRewrite: {
			// 		'^/cmsapi/web': '/web'
			// 	},
			// 	onProxyRes(proxyRes,req) {
			// 		const realUrl = new URL(req.url || '', 'http://192.168.51.181:8080');
			// 		proxyRes.headers['x-real-url'] = realUrl;
			// 	}
			// },
			// '/cmsapi/admin': {
			// 	target: 'http://192.168.51.181:8080',
			// 	ws: false,//socket
			// 	secure: false,// url是否https
			// 	changeOrigin: true, //是否跨域
			// 	pathRewrite: {
			// 		'^/cmsapi/admin': '/admin'
			// 	},
			// 	onProxyRes(proxyRes,req) {
			// 		const realUrl = new URL(req.url || '', 'http://192.168.51.181:8080');
			// 		proxyRes.headers['x-real-url'] = realUrl;
			// 	}
			// },
		},
		client: {
			overlay: false
		},
		// headers: {
		// 	"Cross-Origin-Opener-Policy": "same-origin",
		// 	"Cross-Origin-Embedder-Policy": "require-corp"
		// }
	},
	css: {
    loaderOptions: {
      sass: {
        additionalData: `@import "@/assets/styles/variables.scss";`,
      },
    },
  },

	chainWebpack: config => {
		// 移除 prefetch 插件
		config.plugins.delete('preload');
		config.plugins.delete('prefetch');
		config.resolve.alias.set('vue-i18n', 'vue-i18n/dist/vue-i18n.cjs.js');
		config.module
			.rule('worker')
			.test(/\.worker\.js$/)
			.use('worker-loader')
			.loader('worker-loader')
			.end();
	},

	configureWebpack: {
		// 文件输出重构
		output: {
			filename: `js/[name].vw.${timeCode}.js`,
			chunkFilename: `js/[name].vw.${timeCode}.js`
			// chunkFilename: `js/[id].vw.js`
		},
		//性能提示
		performance: {
			hints: false
		},
		optimization: {
			splitChunks: {
				chunks: "all",
				automaticNameDelimiter: '~',
				name: "scuiChunks",
				cacheGroups: {
					//第三方库抽离
					vendor: {
						name: "modules",
						test: /[\\/]node_modules[\\/]/,
						priority: -10
					},
					elicons: {
						name: "elicons",
						test: /[\\/]node_modules[\\/]@element-plus[\\/]icons-vue[\\/]/
					},
					tinymce: {
						name: "tinymce",
						test: /[\\/]node_modules[\\/]tinymce[\\/]/
					},
					echarts: {
						name: "echarts",
						test: /[\\/]node_modules[\\/]echarts[\\/]/
					},
					xgplayer: {
						name: "xgplayer",
						test: /[\\/]node_modules[\\/]xgplayer.*[\\/]/
					},
					codemirror: {
						name: "codemirror",
						test: /[\\/]node_modules[\\/]codemirror[\\/]/
					}
				}
			}
		}
	}
})
